//
//  FriendsShareView.swift
//  koolcar
//
//  Created by kuaimaxiansheng on 2023/10/5.
//

import UIKit

class FriendsShareView: UIView {
    var bgView: UIView!
    var titleLab: UILabel!
    var contentLab: UILabel!
    var timeLab: UILabel!
    var applicationBtn: UIButton!
    var hideBtn: UIButton!
    var btn: UIButton!
    var registerBtn: UIButton!
    var model: PromotionDetailsDataModel!
//    var shareActivityBlock: BlockShareActivity!
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        let contor = UIControl(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: kScreenHeight))
        contor.backgroundColor = .black
        contor.alpha = 0.7
        contor.addTarget(self, action: #selector(close), for: .touchUpInside)
        self.addSubview(contor)
        
        self.bgView = UIView(frame: CGRect(x: 30, y: (kScreenHeight - 366)/2, width: kScreenWidth - 60, height: 366))
        self.bgView.backgroundColor = .white
        self.bgView.layer.cornerRadius = 8
        self.bgView.clipsToBounds = true
        self.addSubview(self.bgView)
        
        
        self.titleLab = UILabel(frame: CGRect(x: 20, y: 40, width: self.bgView.frame.size.width - 40, height: 20))
        self.titleLab.font = UIFont(name: Theme.PingFangSCRegular, size: 18)
        self.titleLab.textColor = Theme.titleLabColor
        self.titleLab.text = MMLocalizedString("reward_sharing", comment: "亲友分享")
        self.titleLab.textAlignment = .center
        self.bgView.addSubview(self.titleLab)
        
        
        self.contentLab = UILabel(frame: CGRect(x: 20, y: self.titleLab.frame.maxY + 40, width: self.bgView.frame.size.width - 40, height: 30))
        self.contentLab.font = UIFont(name: Theme.PingFangSCMedium, size: 21)
        self.contentLab.textColor = Theme.titleLabColor
        self.contentLab.text = "满100减20"
        self.contentLab.textAlignment = .center
        self.bgView.addSubview(self.contentLab)
        
        
        self.timeLab = UILabel(frame: CGRect(x: 20, y: self.contentLab.frame.maxY + 10, width: self.bgView.frame.size.width - 40, height: 30))
        self.timeLab.font = UIFont(name: Theme.PingFangSCRegular, size: 13)
        self.timeLab.textColor = Theme.titleLabColor
        self.timeLab.text = "2023-1-1至2023-12-12"
        self.timeLab.textAlignment = .center
        self.bgView.addSubview(self.timeLab)
        
        
        self.applicationBtn = UIButton(frame: CGRect(x: (kScreenWidth - 30)/2, y:   self.timeLab.frame.maxY + 10, width: 30, height: 22))
        self.applicationBtn.setTitleColor(Theme.titleLabColor, for: .normal)
        self.applicationBtn.titleLabel?.font = UIFont.init(name: Theme.PingFangSCRegular, size: 11)
        self.applicationBtn.setTitle(MMLocalizedString("application", comment: "只适用于商城产品"), for: .normal)
        self.applicationBtn.isUserInteractionEnabled = false
        self.applicationBtn.layer.borderWidth = 1
        self.applicationBtn.layer.cornerRadius = 11
        self.applicationBtn.clipsToBounds = true
        self.applicationBtn.layer.borderColor = Theme.titleLabColor.cgColor
        self.applicationBtn.titleEdgeInsets = UIEdgeInsets(top: 0, left: 16, bottom: 0, right: 16)
        self.bgView.addSubview(self.applicationBtn)
        
        
        let ww = XTManageCenter.countBtnWidth(btn: self.applicationBtn) + 32
        self.applicationBtn.snp.makeConstraints { make in
            make.top.equalTo(self.timeLab.snp.bottom).offset(10)
            make.centerX.equalTo(self.bgView.snp.centerX)
            make.width.equalTo(ww)
            make.height.equalTo(22)
        }
   
        
        self.btn = UIButton(frame: CGRect(x: 58, y: self.bgView.frame.size.height - 91, width: self.bgView.frame.size.width - 116, height: 46))
        self.btn.setTitleColor(.white, for: .normal)
        self.btn.titleLabel?.font = UIFont.init(name: Theme.PingFangSCMedium, size: 16)
        self.btn.layer.cornerRadius = 8
        self.btn.layer.masksToBounds = true
        self.btn.backgroundColor = Theme.mainColor
        self.btn.addTarget(self, action: #selector(btnAntion), for: .touchUpInside)
        self.bgView.addSubview(self.btn)
       
        
        
        if UserModule.shared.isLogined == true{
            self.btn.setTitle(MMLocalizedString("please_pick_up", comment: "请即领取"), for: .normal)
        }else{
            self.btn.setTitle(MMLocalizedString("please_login_obtain", comment: "请登录获取"), for: .normal)
            self.registerBtn = UIButton(frame: CGRect(x: 58, y: self.btn.frame.maxY , width: self.bgView.frame.size.width - 116, height: 30))
            self.registerBtn.setTitleColor(Theme.mainColor, for: .normal)
            self.registerBtn.titleLabel?.font = UIFont.init(name: Theme.PingFangSCRegular, size: 12)
            self.registerBtn.addTarget(self, action: #selector(registerBtnAntion), for: .touchUpInside)
            self.registerBtn.setTitle(MMLocalizedString("register_member", comment: "注册Kool Car会员"), for: .normal)
            self.bgView.addSubview(self.registerBtn)
        }

        
        self.hideBtn = UIButton(frame: CGRect(x: (kScreenWidth - 30)/2, y:   self.bgView.frame.maxY + 20, width: 30, height: 30))
        self.hideBtn.setImage(UIImage(named: "icon_hide"), for: .normal)
        self.hideBtn.addTarget(self, action: #selector(close), for: .touchUpInside)
        self.addSubview(self.hideBtn)
        
    }
    
    //领取分享的推广活动积分
    func requestGetShareCouponIntegral() {
        let param = ["id": "\(self.model?.id ?? 0)"]
        XTRequestCenter.shared.sendRequestMappableTargetObject(target: APIManager.getShareCouponIntegral(param: param),objectType: CommondResModel.self) { data in
            let resultObj = data as! CommondResModel
            if resultObj.code == "1" {
                self.btn.isUserInteractionEnabled = false
                self.btn.setTitle(MMLocalizedString("already_received", comment: "已领取"), for: .normal)
                self.btn.alpha = 0.5
            }
        }error: { err in
            
        }
    }
    
    
    
    
    //setModel
    func setModel(model: PromotionDetailsDataModel){
        self.model = model
        self.contentLab.text = model.name ?? ""
        self.timeLab.text = model.validEnd
        if model.couponType == 0{
            self.applicationBtn.isHidden = true
            self.applicationBtn.setTitle(MMLocalizedString("application", comment: "只适用于商城产品"), for: .normal)
        }else{
            self.applicationBtn.isHidden = true
        }
    }
    
    
    
    @objc func close(){
        UIView.animate(withDuration: 0.3, animations: {() -> Void in
            self.transform = CGAffineTransform(scaleX: 1.5, y: 1.5)
            self.backgroundColor = UIColor.clear
            self.alpha = 0
        }, completion: {(_ finished: Bool) -> Void in
            self.removeFromSuperview()
        })
    }
    
    @objc func btnAntion(){
        if UserModule.shared.isLogined == false{
            let vc = LoginViewController()
            let loginNavi = BaseNavigationController.init(rootViewController: vc)
            loginNavi.modalPresentationStyle = .fullScreen
            self.viewController().present(loginNavi, animated: true)
        }else{
            self.requestGetShareCouponIntegral()
        }
    }
    
    @objc func registerBtnAntion(){
        
        guard let tabbarVC = UIApplication.shared.keyWindow?.rootViewController as? BaseTabbarController else {
            return
        }
        let vc = RegisterViewController()
        let loginNavi = BaseNavigationController.init(rootViewController: vc)
        loginNavi.modalPresentationStyle = .fullScreen
        tabbarVC.present(loginNavi, animated: true)
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
